package cn.wolfcode.p2p.hosting.util;


import cn.wolfcode.p2p.base.exception.CustomException;
import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

public class HostingUtil {
        public static final String PARTNER_ID= "200004595271";
        public static final String INPUT_CHARSET= "UTF-8";
        public static final String SIGN_TYPE= "RSA";
        public static final String URL_MGS = "http://localhost:1111/mgs/gateway.do";
        public static final String URL_MAS= "http://localhost:1111/mas/gateway.do";
        public static final String ENCODE_PULIC_KEY= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCDWwzhCg0z68cPPsEWBBsMQhHakIhd1uY5pwKZJU8PfSNtzrVgb6shzaKIu9mo72odS0dYIjgMEqopsUFYT95UCQq7/ob4zw29RKs+eDjMybCEuDLG1s1uPi+3nlurEIJg9sUWveb8GIyTOrE6m11qceRwgBNptRk0e1YTRvu9fwIDAQAB";
        public static final String SIGN_PUBLICKEY= "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSasbp8h4MVjdmk0madowGEzsBg3rkvw5KJZN4Ov7L54VWAGJ/IsoyeRaOmEHAs9xdft2Bo6tSNZiCkYtxmkaDd/Ofb7gnaPZ1haMBewYHqTUqBmWO7smFXOhDt/k3XgJb3QnHhJ4P2t4UaA3Iy27F3VfSsEn6As3aEXoMvshn9QIDAQAB";
        public static final String SIGN_PRIVATEKEY= "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAJJqxunyHgxWN2aTSZp2jAYTOwGDeuS/Dkolk3g6/svnhVYAYn8iyjJ5Fo6YQcCz3F1+3YGjq1I1mIKRi3GaRoN3859vuCdo9nWFowF7BgepNSoGZY7uyYVc6EO3+TdeAlvdCceEng/a3hRoDcjLbsXdV9KwSfoCzdoRegy+yGf1AgMBAAECgYAHaRoaOyPkMfTp3yiJhdh+cji5UeNFcpoRjKdTHYFJ5rTr7mcN0j/zoAdRPkmNrEzJ+qLRbew84/ONIDqzjEBnUMRyWbq9rCr/yodld6uY8yQvXONGukv0mDArnsB+0HswkZ4oIMdHyw8EiCjcO+nBE63nc3ejw9XjBjwuUP/PwQJBAPtwxlwu9ORvn5LInQx/xEw6LWRn1kHuzU2V8QPuaZ1u/0MJ0Z1MqtEdWd4cUkox5etxpjAK8Ok7XUgfwcPdEjECQQCVEnep8vpDBxvokuz0bHARzGjUuZpMysBiTmbaUEwsAeCDj+rfQohISCF7GEDOargqKdlZ9XeBU5kZ/PEhf50FAkEA4r9/0o/x/rN/ByLtJeFux4NLfhl6Cblt4YLPd8kf6362qeEH7D/AZ5Z9faSyvQAkpN+3i+nB+cK0S59/4L7TcQJAeedtNew93Xw9xVYJMeRPTS7Ed1kEJlITOxD0KQlLER3D1LJnFoXY3osl3fy6WlKJIemxFVCXlomfhNIE+ijaZQJBANIxZSIC1qD9CA/RzPgxvpu0tjBN0u8Gak4lnUKtmBoBAyILoKkJ4ctElwqCpG4SAMLz6keWUYTUxXjrsR9sXiE=";

        public static Map<String,String> getBaseParams(){
                Map<String,String> map = new HashMap<>();
                map.put("version","1.0");
                map.put("request_time",getTimeString());
                map.put("partner_id",PARTNER_ID);
                map.put("_input_charset",INPUT_CHARSET);
                map.put("sign_type",SIGN_TYPE);
                map.put("sign_version","1.0");
                return map;
        }
        public static String getTimeString(){
                SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
                return format.format(new Date());
        }

        public static String getHostingIdentityId(Long id) {
                return "wolfcode"+id;
        }


        public static String requestHandlerToString(Map<String,String> params,boolean isMgs){
                String signString = Tools.createLinkString(params, false, true);
                String sign = null;
                try {
                        sign = SignUtil.sign(signString, HostingUtil.SIGN_TYPE, HostingUtil.SIGN_PRIVATEKEY, HostingUtil.INPUT_CHARSET);
                } catch (Exception e) {
                        e.printStackTrace();
                }
                params.put("sign",sign);
                String linkString = Tools.createLinkString(params, true, false);
                //把参数打包成需要的格式
                //考虑签名加密
                //发送请求
                String url = isMgs?URL_MGS:URL_MAS;
                String s = CallServiceUtil.sendPost(url, linkString);
                //执行解码
                try {
                        s = URLDecoder.decode(s, HostingUtil.INPUT_CHARSET);
                } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                }
                System.out.println(s);
                //将响应回来的字符串解析成一个MAP对象
                /*  if(isHtml){return s};*/
                return s;

        }

        public static Map<String,String> requestHandler(Map<String,String> params,boolean isMgs){
                try {
                        String s = requestHandlerToString(params, isMgs);
                        Map<String,String> map = JSON.parseObject(s, Map.class);

                        if(!map.get("response_code").equals("APPLY_SUCCESS")){
                                throw new CustomException(map.get("response_message"));
                        }
                        checkSign(map);
                        return map;
                } catch (Exception e) {
                        e.printStackTrace();
                }

                return null;
        }

        public static void checkSign(Map<String,String> map){

                try {
                        String callBackSignString = Tools.createLinkString(map, false, true);
                        boolean   checkSign = SignUtil.Check_sign(callBackSignString, map.get("sign"), HostingUtil.SIGN_TYPE, HostingUtil.SIGN_PUBLICKEY, HostingUtil.INPUT_CHARSET);
                        if(!checkSign){
                                throw new CustomException("系统异常!");
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

        public static Map<String,String> requestToMap(HttpServletRequest request){
                Enumeration<String> parameterNames = request.getParameterNames();
                Map<String,String> map = new HashMap<>();
                while(parameterNames.hasMoreElements()){
                        String s = parameterNames.nextElement();
                        String parameter = request.getParameter(s);
                        map.put(s,parameter);
                        System.out.println(s+":"+parameter);
                }
                return map;
        }

}
